package com.ruoqing.admin.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoqing.admin.api.LogHandleService;
import com.ruoqing.admin.api.UserHandleService;
import com.ruoqing.admin.api.UserStandardService;
import com.ruoqing.admin.dao.UserHandleDao;
import com.ruoqing.admin.feign.UserServiceFeign;
import com.ruoqing.core.constant.Constants;
import com.ruoqing.core.constant.ModuleConstants;
import com.ruoqing.core.domain.rqadmin.BanUserPo;
import com.ruoqing.core.dto.BaseResponse;
import com.ruoqing.core.dto.admin.BanUserDto;
import com.ruoqing.core.dto.admin.BanUserResponse;
import com.ruoqing.core.enums.UserQueryEnum;
import com.ruoqing.log.annotation.BaseLog;
import com.ruoqing.log.enums.BusinessType;
import com.ruoqing.utils.common.DateUtils;
import com.ruoqing.utils.common.ResultJudgeUtil;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
	* @author YaoXian
	* @title: UserStandardServiceImpl
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 后台管理中心 - 用户行为规范业务层实现类
	* @date 2020-11-16 17:35
	*/
@RestController
@Slf4j
public class UserStandardServiceImpl implements UserStandardService {
		
		@Resource
		private UserHandleDao userHandleDao;
		
		@Resource
		private LogHandleService logHandleService;
		
		@Resource
		private UserHandleService userHandleService;
		
		@Resource
		private UserServiceFeign userServiceFeign;
		
		@Override
		public BanUserPo findOffendUser(String phoneNumber) {
				QueryWrapper<BanUserPo> queryWrapper = new QueryWrapper<BanUserPo>();
				queryWrapper.eq("user_phoneNumber",phoneNumber);
				return userHandleDao.selectOne(queryWrapper);
		}
		
		@Override
		@BaseLog(title = "添加黑名单",module = ModuleConstants.APP_ADMIN,businessType = BusinessType.INSERT)
		@GlobalTransactional(rollbackFor = Exception.class,name = "addBlackUser")
		public BaseResponse addBlackUser(String phoneNumber,Boolean whetherDelete) {
				String status;
				String msg;
				BanUserPo blackUser = new BanUserPo();
				blackUser.setPhoneNumber(phoneNumber);
				blackUser.setBanTime(new Date(new java.util.Date().getTime()));
				int insert = userHandleDao.insert(blackUser);
				if (insert>0){
						status = Constants.responseOk;
						msg = "添加黑名单成功,未删除用户!";
						userHandleService.removeRedisBanUser(UserHandleServiceImpl.redisKeys);
						logHandleService.removeRedisLog(LogHandleServiceImpl.redisKeys);
						log.info("手机号为"+phoneNumber+"的用户添加到黑名单成功!");
						if (whetherDelete){
								userServiceFeign.deleteUserBySelect(phoneNumber, UserQueryEnum.phoneNumber);
								msg = "添加黑名单成功,并删除用户!";
						}
				}else {
						status = Constants.responseError;
						msg = "添加黑名单失败,请稍后重试!";
				}
				return BaseResponse.buildResponse(status,msg);
		}
		
		@Override
		@BaseLog(title = "移除黑名单",module = ModuleConstants.APP_ADMIN,businessType = BusinessType.DELETE)
		@Transactional(rollbackFor = Exception.class)
		public BaseResponse removeBlackUser(Integer blackId) {
				int result = userHandleDao.deleteById(blackId);
				userHandleService.removeRedisBanUser(UserHandleServiceImpl.redisKeys);
				logHandleService.removeRedisLog(LogHandleServiceImpl.redisKeys);
				return ResultJudgeUtil.baseResultJudge(result);
		}
		
		@Override
		public BanUserResponse blurQueryBanUser(String queryParam) {
				String status;
				String msg;
				QueryWrapper<BanUserPo> queryWrapper = new QueryWrapper<BanUserPo>();
				queryWrapper.like(!StringUtils.isEmpty(queryParam),"user_phoneNumber",queryParam);
				List<BanUserPo> blackListEntities = userHandleDao.selectList(queryWrapper);
				ArrayList<BanUserDto> list = new ArrayList<BanUserDto>(blackListEntities.size());
				if (!blackListEntities.isEmpty()){
						for (BanUserPo blackUser : blackListEntities) {
								BanUserDto banUserDto = new BanUserDto();
								BeanUtils.copyProperties(blackUser,banUserDto);
								banUserDto.setBanDateFormat(DateUtils.dateTimeToStr(blackUser.getBanTime()));
								list.add(banUserDto);
						}
						status = Constants.responseOk;
						msg = "共检索到" + blackListEntities.size() + "条数据!";
				}else {
						status = Constants.responseError;
						msg = "没有检索到数据!";
				}
				return BanUserResponse.buildBanUserResponse(status,msg,list);
		}
		
}
